package com.qyb;

/**
 * <strong>实现功能:</strong>
 * <p>
 *     环形队列-链表
 * </p>
 *
 * @author 秦有宝(qinyoubao)
 * @version 1.0.0  2019/11/15 15:14
 */
public class Circle2 {

    public static void main(String[] args) {
        LC lc = new LC(5);
        System.out.println(lc.isEmpty());
        lc.add("1");
        lc.add("2");
        lc.add("3");
        lc.add("4");
        lc.add("5");
        lc.add("6");

        lc.print();

        System.out.println( lc.take());
        System.out.println( lc.take());
        System.out.println( lc.take());
        System.out.println( lc.take());
        System.out.println( lc.take());
        System.out.println( lc.take());

        System.out.println(lc.isFull());
        System.out.println(lc.isEmpty());
        lc.add("6");
        lc.add("5");
        lc.add("4");
        lc.add("3");
        lc.add("2");
        lc.add("1");
        lc.print();
    }
}


class LC{

    private LNode head;
    private LNode tail;
    private int size;
    public LC(int size){
        this.size = size;
        head = new LNode("");
        tail = head;
        LNode temp = head;
        for (int i = 0; i < size; i++) {
            temp.setNext(new LNode(""));
            temp = temp.getNext();
        }
        temp.setNext(head);
    }


    public boolean isEmpty(){
        if(head == tail){
            return true;
        }
        return false;
    }

    public boolean isFull(){
        if(tail.getNext() == head){
            return true;
        }
        return false;
    }

    public void add(String v){
        if(isFull()){
            return;
        }
        tail.setV(v);
        tail = tail.getNext();
    }

    public String take(){
        if(isEmpty()){
            return "";
        }
        String re = head.getV();
        head = head.getNext();
        return re;
    }

    public void print(){
        if(isEmpty()){
            return;
        }

        LNode temp = head;
        while (temp!= tail){
            System.out.println(temp.getV());
            temp = temp.getNext();
        }



    }
}

class LNode{
    private String v;
    private LNode next;

    public LNode(String v) {
        this.v = v;
    }

    public String getV() {
        return v;
    }

    public void setV(String v) {
        this.v = v;
    }

    public LNode getNext() {
        return next;
    }

    public void setNext(LNode next) {
        this.next = next;
    }
}
